package com.ljl.order.service.impl;

import com.ljl.basic.exception.BusinessException;
import com.ljl.basic.service.IBaiduAiAuditService;
import com.ljl.basic.service.impl.BaseServiceImpl;
import com.ljl.order.domain.Product;
import com.ljl.order.domain.ProductDetail;
import com.ljl.order.mapper.ProductDetailMapper;
import com.ljl.order.mapper.ProductMapper;
import com.ljl.order.service.IProductService;
import com.ljl.pet.domain.PetOnlineAuditLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Override
    public void save(Product product) {
        super.save(product);
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null){
            productDetail.setProductId(product.getId());
            productDetailMapper.save(productDetail);
        }
    }

    @Override
    public void del(Long id) {
        super.del(id);
        productDetailMapper.delProductDetailByProductId(id);

    }

    @Override
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null){
            productDetail.setProductId(product.getId());
            productDetailMapper.updateProductDetailByProductId(productDetail);
        }
    }

    @Override
    public void onsale(List<Long> ids, HttpServletRequest request) {
        for (Long id : ids) {
            Product product = productMapper.findById(id);
            Boolean textBoo = baiduAiAuditService.textAudit(product.getName());
            Boolean imageBoo = baiduAiAuditService.imageAudit(product.getResources());
            PetOnlineAuditLog log = new PetOnlineAuditLog();
            if (textBoo && imageBoo){
                product.setState(1L);
                product.setOnsaletime(new Date());
                productMapper.update(product);
            } else {
                product.setState(0L);
                product.setOffsaletime(new Date());
                productMapper.update(product);
                throw new BusinessException("图片或名字不合规！");
            }
        }
    }

    @Override
    public void offsale(List<Long> ids) {
        Date offsaletime = new Date();
        productMapper.offsale(offsaletime,ids);
    }
}
